package crp.core.academic.domain.repositories;

import com.qwlabs.lang.C2;
import crp.core.academic.domain.models.AcademicTermEntity;
import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;
import io.quarkus.panache.common.Parameters;
import io.quarkus.panache.common.Sort;

import javax.enterprise.context.ApplicationScoped;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Stream;

@ApplicationScoped
public class AcademicTermRepository implements PanacheRepositoryBase<AcademicTermEntity, String> {
    public Stream<AcademicTermEntity> findOrderedByYears(Set<Integer> years) {
        if (C2.isEmpty(years)) {
            return Stream.empty();
        }
        return find("year in (:years)", Sort.ascending("startAt"), Parameters.with("years", years)).stream();
    }

    public Stream<AcademicTermEntity> findByIds(Collection<String> ids) {
        if (C2.isEmpty(ids)) {
            return Stream.empty();
        }
        return find("id in (:ids)", Parameters.with("ids", ids)).stream();
    }

    public AcademicTermEntity findCurrentTerm() {
        return findAll(Sort.descending("startAt")).firstResult();
    }
}
